package com.changgou.order.service.impl;


import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.pojo.OrderItem;

import com.changgou.order.pojo.OrderItem1;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -----------------------------------
 * MYCODE ANNOTATION         *
 * ------------------------------------
 *
 * @date: 2020-7-31 14:50            *
 * @auther: WenJ                     *
 * @Description: *
 * <p>
 * ----------------------------------
 */

@Service
public class CartServiceImpl implements CartService {

    private static final String CART = "cart_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加购物车
     *
     * @param skuId
     * @param num
     * @param username
     */
    @Override
    public void addCart(String skuId, Integer num, String username) {

        // 查询redis中是否有相对应的商品信息
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);

        if (orderItem != null) {
            // 如果存在则更新商品的数量和价钱
            if (orderItem.getNum() <= 0) {
                // 从redis中将商品删除
                redisTemplate.boundHashOps(CART + username).delete(skuId);

                return;
            }

            orderItem.setNum(orderItem.getNum() + num);
            orderItem.setMoney(orderItem.getNum() * orderItem.getMoney());
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getMoney());


        } else {
            // 如果不存在，则添加
            Sku sku = skuFeign.findById(skuId).getData();
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();


            // 封装orderItem
            orderItem = this.sku2OrderItem(spu, sku, num);

        }

        // 将orderItem放入redis中
        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);

    }


    /**
     * 查询购物车
     *
     * @param username
     * @return
     */
    @Override
    public Map list(String username) {

        Map map = new HashMap();
        Integer totalNum = 0;
        Integer totalMoney = 0;

        // 根据用户名字查询redis
        String key = CART + username;

        List<OrderItem> list = redisTemplate.boundHashOps(key).values();
        map.put("orderItemList", list);

        for (OrderItem orderItem : list) {

            totalNum += orderItem.getNum();
            totalMoney += orderItem.getMoney();

        }

        map.put("totalNum", totalNum);
        map.put("totalMoney", totalMoney);


        return map;
    }


    @Override
    public Map BuyList(List<OrderItem1> list) {

        Map map = new HashMap();
        Integer totalNum = 0;
//        Integer totalMoney = 0;
        String key = CART + tokenDecode.getUserInfo().get("username");

        redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));


        List<OrderItem> orderItemList = new ArrayList<>();
        for (OrderItem1 orderItem : list) {
            String skuId = orderItem.getSkuId();
            OrderItem o = (OrderItem) redisTemplate.boundHashOps(key).get(skuId);

            orderItemList.add(o);

            totalNum += orderItem.getNum();


        }

        map.put("orderItemList", orderItemList);
        map.put("totalNum", totalNum);


        return map;
    }


    /**
     * 将sku封装进orderItem中
     *
     * @param spu
     * @param sku
     * @param num
     * @return
     */
    private OrderItem sku2OrderItem(Spu spu, Sku sku, Integer num) {

        OrderItem orderItem = new OrderItem();

        orderItem.setSpuId(spu.getId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);

        orderItem.setMoney(num * orderItem.getPrice());
        orderItem.setPayMoney(num * orderItem.getPrice());

        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num);

        // 分类信息
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());

        return orderItem;
    }
}
